<?php
App::uses('AppHelper', 'View/Helper');
class ArticlesHelper extends AppHelper {
	public $helpers = array('Html');
	
	public function display($homepage = false){
		$articles = $this->_View->get('articles');
		$paging = $this->params->paging;
		$pages = $paging['Article']['pageCount'];
		$count = $paging['Article']['count'];

		if(is_array($articles) && !empty($articles) && $count >1 && $pages >= 1 || is_array($articles) && !empty($articles) && $homepage == 1){
			
			echo $this->_View->element('Article.display_articles_head');
			foreach ($articles as $key => $a) {
				echo $this->_View->element('Article.display_articles',
					array(
						'article_lengt'=>400,
						'article_key'=>$key,
						'article_id'=>$a['Article']['id'],
						'article_name'=>$a['Article']['name'],
						'article_modified' => $a['Article']['modified'],
						'article_autor' => $a['Article']['user_id'],
						'article_content' => $a['Article']['content'],
						'article_list_content' => $a['Article']['list_content'],
						'article_slug' => $a['Article']['slug'],
						'article_part' => true,
						'gallery' => (isset($a['Gallery'])) ? $a['Gallery'] : false,
						'download_files' => (isset($a['Download'])) ? $a['Download'] : false,
						'rating' => (isset($a['Rating'])) ? $a['Rating'] : false,
						'display_name'=>$a['Article']['display_name'],
						'display_autor'=>$a['Article']['display_autor'],
						'display_date'=>$a['Article']['display_date'],
						'display_gallery'=>$a['Article']['display_gallery'],
						'display_rating'=>$a['Article']['display_rating'],
						'display_comments'=>(isset($a['Article']['display_comments'])) ? $a['Article']['display_comments'] : false
				));
			}
			echo $this->_View->element('Article.display_articles_fotter');
			
		}else if(is_array($articles) && !empty($articles) && count($articles) == 1){
			foreach ($articles as $key => $a) {
				echo $this->_View->element('Article.display_one_article',
						array(
								'article_key'=>$key,
								'article_id'=>$a['Article']['id'],
								'article_name'=>$a['Article']['name'],
								'article_modified' => $a['Article']['modified'],
								'article_autor' => $a['Article']['user_id'],
								'article_content' => $a['Article']['content'],
								'article_list_content' => $a['Article']['list_content'],
								'article_slug' => $a['Article']['slug'],
								'article_part' => true,
								'gallery' => (isset($a['Gallery'])) ? $a['Gallery'] : false,
								'download_files' => (isset($a['Download'])) ? $a['Download'] : false,
								'rating' => (isset($a['Rating'])) ? $a['Rating'] : false,
								'display_name'=>$a['Article']['display_name'],
								'display_autor'=>$a['Article']['display_autor'],
								'display_date'=>$a['Article']['display_date'],
								'display_gallery'=>$a['Article']['display_gallery'],
								'display_rating'=>$a['Article']['display_rating'],
								'display_comments'=>(isset($a['Article']['display_comments'])) ? $a['Article']['display_comments'] : false
								
						));
			}
		}else{
			$this->_View->set('title_for_content',"Brak informacji o stronie");
			echo $this->_View->element('Article.no-article');
		}
	}

	public function homepage(){
		$articles = $this->_View->get('h_articles');

		if(is_array($articles) && !empty($articles)){
			echo $this->_View->element('Article.display_articles_homepage_head');
			foreach ($articles as $key => $a) {
				echo $this->_View->element('Article.display_h_articles',
						array(
								'article_lengt'=>400,
								'article_key'=>$key,
								'article_id'=>$a['Article']['id'],
								'article_name'=>$a['Article']['name'],
								'article_modified' => $a['Article']['modified'],
								'article_autor' => $a['Article']['user_id'],
								'article_content' => $a['Article']['content'],
								'article_list_content' => $a['Article']['list_content'],
								'article_slug' => $a['Article']['slug'],
								'article_part' => true,
								'gallery' => (isset($a['Gallery'])) ? $a['Gallery'] : false,
								'download_files' => (isset($a['Download'])) ? $a['Download'] : false,
								'rating' => (isset($a['Rating'])) ? $a['Rating'] : false,
								'display_name'=>$a['Article']['display_name'],
								'display_autor'=>$a['Article']['display_autor'],
								'display_date'=>$a['Article']['display_date'],
								'display_gallery'=>$a['Article']['display_gallery'],
								'display_rating'=>$a['Article']['display_rating'],
								'display_comments'=>(isset($a['Article']['display_comments'])) ? $a['Article']['display_comments'] : false
						));
			}
			echo $this->_View->element('Article.display_articles_homepage_fotter');
		}else{
			echo $this->_View->element('Article.no-article');
		}
	}
	
	public function homepage_one_article(){
		$articles = $this->_View->get('articles');
		if(is_array($articles) && !empty($articles)){
			foreach ($articles as $key => $a) {
				echo $this->_View->element('Article.display_one_article_homepage',
						array(
								'article_lengt'=>400,
								'article_key'=>$key,
								'article_id'=>$a['Article']['id'],
								'article_name'=>$a['Article']['name'],
								'article_modified' => $a['Article']['modified'],
								'article_autor' => $a['Article']['user_id'],
								'article_content' => $a['Article']['content'],
								'article_list_content' => $a['Article']['list_content'],
								'article_slug' => $a['Article']['slug'],
								'article_part' => true,
								'gallery' => (isset($a['Gallery'])) ? $a['Gallery'] : false,
								'download_files' => (isset($a['Download'])) ? $a['Download'] : false,
								'rating' => (isset($a['Rating'])) ? $a['Rating'] : false,
								'display_name'=>$a['Article']['display_name'],
								'display_autor'=>$a['Article']['display_autor'],
								'display_date'=>$a['Article']['display_date'],
								'display_gallery'=>$a['Article']['display_gallery'],
								'display_rating'=>$a['Article']['display_rating'],
								'display_comments'=>(isset($a['Article']['display_comments'])) ? $a['Article']['display_comments'] : false
						));
			}
		}
	}
	
	
	public function view(){
		$article = $this->_View->get('article');
		if(is_array($article)){
			echo $this->_View->element('Article.article',
			array(
						'article_id'=>$article['Article']['id'],
						'article_name'=>$article['Article']['name'],
						'article_modified' => $article['Article']['modified'],
						'article_autor' => $article['Article']['user_id'],
						'article_content' => $article['Article']['content'],
						'article_list_content' => $article['Article']['list_content'],
						'article_slug' => $article['Article']['slug'],
						'article_part' => false,
						'gallery' => (isset($article['Gallery'])) ? $article['Gallery'] : false,
						'download_files' => (isset($article['Download'])) ? $article['Download'] : false,
						'rating' => (isset($article['Rating'])) ? $article['Rating'] : false,
						'display_name'=>$article['Article']['display_name'],
						'display_autor'=>$article['Article']['display_autor'],
						'display_date'=>$article['Article']['display_date'],
						'display_gallery'=>$article['Article']['display_gallery'],
						'display_rating'=>$article['Article']['display_rating'],
						'display_comments'=>(isset($article['Article']['display_comments'])) ? $article['Article']['display_comments'] : false
			));
		}else{
			echo $this->_View->element('Article.no-article');
		}
	}
	
	public function review(){
		$article = $this->_View->get('article');
		if(is_array($article)){
			echo $this->_View->element('Article.article_review',
					array(
							'article_id'=>$article['Article']['id'],
							'article_name'=>$article['Article']['name'],
							'article_description'=>$article['Article']['descriptions'],
							'article_modified' => $article['Article']['modified'],
							'article_autor' => $article['Article']['user_id'],
							'article_content' => $article['Article']['content'],
							'article_list_content' => $article['Article']['list_content'],
							'article_slug' => $article['Article']['slug'],
							'article_part' => false,
							'gallery' => (isset($article['Gallery'])) ? $article['Gallery'] : false,
							'download_files' => (isset($article['Download'])) ? $article['Download'] : false,
							'rating' => (isset($article['Rating'])) ? $article['Rating'] : false,
							'display_name'=>$article['Article']['display_name'],
							'display_autor'=>$article['Article']['display_autor'],
							'display_date'=>$article['Article']['display_date'],
							'display_gallery'=>$article['Article']['display_gallery'],
							'display_rating'=>$article['Article']['display_rating'],
							'display_comments'=>(isset($article['Article']['display_comments'])) ? $article['Article']['display_comments'] : false
					));
		}else{
			echo $this->_View->element('Article.no-article');
		}
	}
	
	public function rating($article_key = null){
		$article = (is_null($article_key)) ? $this->_View->get('article') : $this->_View->get('articles');
		if(is_array($article) && !is_null($article_key)){
			echo $this->_View->element('Article.rating',
			array(
					'article_id'=>$article[$article_key]['Article']['id'],
					'article_name'=>$article[$article_key]['Article']['name'],
					'article_slug'=>$article[$article_key]['Article']['slug'],
					'rating' => (isset($article[$article_key]['Rating'])) ? $article[$article_key]['Rating'] : false
			));
		}else{
			echo $this->_View->element('Article.rating',
			array(
							'article_id'=>$article['Article']['id'],
							'article_name'=>$article['Article']['name'],
							'article_slug'=>$article['Article']['slug'],
							'rating' => (isset($article['Rating'])) ? $article['Rating'] : false
			));
		}
	}
	
	public function comments($article_key = null){
		$article = (is_null($article_key)) ? $this->_View->get('article') : $this->_View->get('articles');
		if(is_array($article) && !is_null($article_key)){
			echo $this->_View->element('Article.comments',
			array(
						'article_id'=>$article[$article_key]['Article']['id'],
						'comments' => (isset($article[$article_key]['Comment'])) ? $article[$article_key]['Comment'] : false
			));
		}else{
			echo $this->_View->element('Article.comments',
			array(
						'article_id'=>$article['Article']['id'],
						'comments' => (isset($article['Comment'])) ? $article['Comment'] : false
			));
		}
	}
	
	public function rating_values($article_key = null){
		$article = (is_null($article_key)) ? $this->_View->get('article') : $this->_View->get('articles');
		if(is_array($article) &&  !is_null($article_key)){
			echo $this->_View->element('Article.rating_values',
			array(
								'article_id'=>$article[$article_key]['Article']['id'],
								'article_name'=>$article[$article_key]['Article']['name'],
								'ratings' => (isset($article[$article_key]['Rating'])) ? $article[$article_key]['Rating'] : false,
								'rating_values' => (isset($article[$article_key]['RatingValue'])) ? $article[$article_key]['RatingValue'] : false,
								'rating_total' => (isset($article[$article_key]['RatingTotal'])) ? $article[$article_key]['RatingTotal'] : 0
			));
		}else{
			echo $this->_View->element('Article.rating_values',
			array(
								'article_id'=>$article['Article']['id'],
								'article_name'=>$article['Article']['name'],
								'ratings' => (isset($article['Rating'])) ? $article['Rating'] : false,
								'rating_values' => (isset($article['RatingValue'])) ? $article['RatingValue'] : false,
								'rating_total' => (isset($article['RatingTotal'])) ? $article['RatingTotal'] : 0
			));
		}
	}
	

	
	
	public function posts($article_key = null){
		$article = (is_null($article_key)) ? $this->_View->get('article') : $this->_View->get('articles');
		if(is_array($article) && !is_null($article_key)){
			echo $this->_View->element('Article.posts',
			array(
							'article_id'=>$article[$article_key]['Article']['id'],
							'posts' => (isset($article[$article_key]['Post'])) ? $article[$article_key]['Post'] : false
			));
		}else{
			echo $this->_View->element('Article.posts',
			array(
							'article_id'=>$article['Article']['id'],
							'posts' => (isset($article['Post'])) ? $article['Post'] : false
			));
		}
	}
	
	/**
	 * display ratings for homepage
	 */
	public function rating_page_index() {
		$page_index = $this->_View->get ( 'page_index' );
// 		if($page_index['RatingTotal'] == 0) return;
		if (is_array ( $page_index ) && ! is_null ( $page_index )) {
			echo $this->_View->element ( 'Article.page_index_rating_values', array (
					'article_id' => null,
					'article_name' => __ ( 'Total ratings', true ),
					'ratings' => (isset ( $page_index ['Rating'] )) ? $page_index ['Rating'] : false,
					'rating_values' => (isset ( $page_index ['RatingValue'] )) ? $page_index ['RatingValue'] : false,
					'rating_groups' => (isset ( $page_index ['RatingGroup'] )) ? $page_index ['RatingGroup'] : false,
					'rating_total' => (isset ( $page_index ['RatingTotal'] )) ? $page_index ['RatingTotal'] : 0,
					'best_articles' => (isset ( $page_index ['BestArticle'] )) ? $page_index ['BestArticle'] : false,
			) );
		}
	}
	
	public function posts_page_index(){
		$page_index = $this->_View->get('page_index');
		if(is_array($page_index) && !is_null($page_index)){
			echo $this->_View->element('Article.page_index_posts',
					array(
							'article_id'=>null,
							'posts' => (isset($page_index['Post'])) ? $page_index['Post'] : false
					));
		}
	}
}